package com.pay.ioopos.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.pay.ioopos.IconFontFactory;
import com.pay.ioopos.R;


public abstract class TipFragment extends AbstractNetworkFragment {

    private View view;
    private Float textSize;
    private TipType type;
    private String msg;
    private Integer msgId;
    private Handler msgHandler;
    private Handler waitHandler;
    private Runnable waitRunnable;
    private Thread waitThread;
    private int waitTimes = 0;
    public TipFragment(TipType type, String msg) {
        this.type = type;
        this.msg = msg;
    }

    public TipFragment(TipType type, int msgId) {
        this.type = type;
        this.msgId = msgId;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(layoutId(), container, false);
        TextView iconView = view.findViewById(R.id.tip_icon);
        iconView.setTypeface(IconFontFactory.getHalflings(getActivity()));

        if(null != textSize) {
            setTextSize(textSize);
        }

        if (null == Looper.myLooper()) {
            Looper.prepare();
        }

        if (null == msg && null != msgId) {
            msg = getString(msgId);
        }

        msgHandler = new Handler();
        msgHandler.post(() -> handleMsg(msg));
        msgHandler.post(() -> handleType(type));

        waitHandler = new Handler();
        waitRunnable = () -> {
            if (!view.isAttachedToWindow()) return;
            TextView textView = view.findViewById(R.id.tip_msg);
            String text = msg + ("...".substring(0, waitTimes%4));
            textView.setText(text);
        };
        if (type == TipType.WAIT) {
            startWait();
        } else {
            handleMsg(msg);
            handleType(type);
        }

        return view;
    }

    @Override
    public void onPause() {
        waitHandler.removeCallbacks(waitRunnable);
        if (null != waitThread) {
            waitThread.interrupt();
        }
        super.onPause();
    }

    protected abstract int layoutId();

    public enum TipType {
        NONE,WAIT,WARN,SUCCESS,FAIL,ERROR,EXPIRED
    }

    public void onChange(TipType type, int msgId) {
        msg = getString(msgId);
        onChange(type, msg);
    }

    public void onChange(TipType type, String msg) {
        onType(type);
        onMsg(msg);
    }

    public void onMsg(int msgId) {
        msg = getString(msgId);
        onMsg(msg);
    }

    public void onMsg(String msg) {
        this.msg = msg;
        if (null != view && view.isAttachedToWindow()) {
            msgHandler.post(() -> handleMsg(msg));
        }
    }

    public void onType(TipType type) {
        if (null != waitThread) {
            waitThread.interrupt();
        }
        this.type = type;
        if (null != view && view.isAttachedToWindow()) {
            msgHandler.post(() -> handleType(type));
        }
    }

    public TipType getType() {
        return type;
    }

    private void handleMsg(String msg) {
        TextView msgView = view.findViewById(R.id.tip_msg);
        msgView.setText(msg);
    }

    private void handleType(TipType type) {
        if (null != waitThread) {
            waitThread.interrupt();
        }
        Context context = getContext();
        if (null == view || null == context) {
            return;
        }
        TextView iconView = view.findViewById(R.id.tip_icon);
        if (type != TipType.NONE) {
            iconView.setVisibility(View.VISIBLE);
        }
        if (type == TipType.WAIT || type == TipType.WARN) {
            iconView.setTextColor(getContext().getColor(R.color.colorWarning));
            iconView.setText(R.string.glyphicon_exclamation_sign);
            if (type == TipType.WAIT) {
                startWait();
            }
        } else if (type == TipType.SUCCESS) {
            iconView.setTextColor(getContext().getColor(R.color.colorSuccess));
            iconView.setText(R.string.glyphicon_ok_sign);
        } else if (type == TipType.FAIL || type == TipType.ERROR || type == TipType.EXPIRED) {
            iconView.setTextColor(getContext().getColor(R.color.colorDanger));
            iconView.setText(R.string.glyphicon_remove_sign);
        }
    }

    private void startWait() {
        waitTimes = 0;
        if (null != waitThread) {
            waitThread.interrupt();
        }
        waitThread = new Thread(() -> {
            try {
                while(true) {
                    waitHandler.removeCallbacks(waitRunnable);
                    waitHandler.post(waitRunnable);
                    waitTimes++;
                    Thread.sleep(700);
                }
            } catch (InterruptedException e) {
            }
        });
        waitThread.start();
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
        if (null == view) {
            return;
        }
        msgHandler.post(() -> {
            TextView iconView = view.findViewById(R.id.tip_icon);
            iconView.setTextSize(textSize);
            TextView textView = view.findViewById(R.id.tip_msg);
            textView.setTextSize(textSize);
        });
    }

    @Override
    public boolean useNetwork() {
        return false;
    }
}
